<?php

namespace App\Kuafu\Order\Model\Clickhouse;

use App\Kuafu\Base\Model\Traits\KfSoftDeletes;
use App\Kuafu\Base\Model\Traits\KfUid;
use App\Kuafu\Customer\Model\CustomerModel;
use App\Kuafu\Order\Enum\CodeEnum;
use App\Kuafu\Order\Model\OrderOperateRecordModel;
use App\Kuafu\Pay\Model\Clickhouse\PayStreamCHModel;
use App\Kuafu\Shop\Model\Shop;
use Illuminate\Database\Eloquent\Concerns\HasTimestamps;
use Kuafu\LaravelClickHouse\Database\Eloquent\Model;

class OrderCHModel extends Model
{
    protected $connection = 'clickhouseOrderAll';
    protected $table      = 'kf_order';
    protected $primaryKey = 'orderId';

    use KfUid;
    use KfSoftDeletes;

    public function orderDetailRel()
    {
        return $this->hasMany(OrderDetailCHModel::class, 'orderId', 'orderId');
    }

    public function orderCouponRel()
    {
        return $this->hasMany(OrderCouponCHModel::class, 'orderId', 'orderId');
    }

    public function orderUserInfoRel()
    {
        return $this->hasOne(OrderUserInfoCHModel::class, 'orderId', 'orderId');
    }

    //模型关联
    public function customerRel()
    {
        return $this->hasOne(CustomerModel::class, 'id', 'userId');
    }

    public function orderCouponExtRel()
    {
        return $this->hasMany(OrderCouponExtCHModel::class, 'orderId', 'orderId');
    }

    public function orderExtRel()
    {
        return $this->hasMany(OrderExternalCHModel::class, 'orderId', 'orderId');
    }

    public function orderExtOneRel()
    {
        return $this->hasOne(OrderExternalCHModel::class, 'orderId', 'orderId');
    }

    public function orderDiscountRel()
    {
        return $this->hasMany(OrderDiscountCHModel::class, 'orderId', 'orderId');
    }

    public function shopRel()
    {
        return $this->hasOne(Shop::class, 'id', 'shopId');
    }

    public function orderPayStreamRel()
    {
        return $this->hasMany(PayStreamCHModel::class, 'orderId', 'orderId');
    }

    public function orderRefundStreamRel()
    {
        return $this->hasMany(PayStreamCHModel::class, 'orderId', 'orderId');
    }

    public function orderOperateRecordRel()
    {
        return $this->hasMany(OrderOperateRecordModel::class, 'orderId', 'orderId');
    }

    public function getSourceTypeTextAttribute(): string
    {
        return CodeEnum::SOURCE_TYPES[$this->sourceType] ?? '';
    }

    public function getOrderStatusTextAttribute(): string
    {
        return CodeEnum::STATUS_CODE_TEXT[$this->orderStatus] ?? '';
    }

    public function getRefundStatusTextAttribute(): string
    {
        return CodeEnum::REFUND_CODE_TEXT[$this->refundStatus] ?? '';
    }

    public function getPayChannelTextAttribute(): string
    {
        return CodeEnum::CHANNEL_CODE_TEXT[$this->payChannel] ?? '';
    }
}
